package learning.homework;

import java.io.File;
import java.io.RandomAccessFile;

public class Ex1 {
	public static void main(String[] args) {
		File srf = new File("D:films/bcd.wmv");
		File dsf = new File("E:/abc.wmv");
		long size = 5 * 1024 * 1024;
		long start = 0;
		long end = 0;
		int i = (int) (srf.length() / size);
		double d = (double)srf.length() / size;
		if (d > i)
			for (int j = 0; j < i + 1; j++) {
				start = end;
				end = (j + 1) * size;
				new MyCopy(start, end, srf, dsf).start();
			}
		else
			for (int j = 0; j < i; j++) {
				start = end;
				end = (j + 1) * size;
				new MyCopy(start, end, srf, dsf).start();
			}
	}

}

class MyCopy extends Thread {
	private long start;
	private long end;
	private File srf;
	private File dsf;

	public MyCopy(long start, long end, File srf, File dsf) {
		super();
		this.start = start;
		this.end = end;
		this.srf = srf;
		this.dsf = dsf;
	}

	@Override
	public void run() {
		try {
			RandomAccessFile sraf = new RandomAccessFile(this.getSrf(), "rw");
			RandomAccessFile draf = new RandomAccessFile(this.getDsf(), "rw");
			sraf.seek(start);
			draf.seek(start);
			int count = 0;
			byte[] bb=new byte[1024*1024];
			int b = 0;
			while ((b = sraf.read(bb)) != -1) {
				draf.write(bb,0,b);
				count+=b;
				if (count ==(end-start))
					break;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public long getStart() {
		return start;
	}

	public void setStart(long start) {
		this.start = start;
	}

	public long getEnd() {
		return end;
	}

	public void setEnd(long end) {
		this.end = end;
	}

	public File getSrf() {
		return srf;
	}

	public void setSrf(File srf) {
		this.srf = srf;
	}

	public File getDsf() {
		return dsf;
	}

	public void setDsf(File dsf) {
		this.dsf = dsf;
	}

}